#！ /bin/bash


if [[ $EUID -ne 0 ]]; then
    echo "Error:This script must be run as root!" 1>&2
    exit 1
fi

imagetool_path=/var/tmp/image_tool
rootfs_path=$imagetool_path/rootfs
packages_path=./packages.txt
present_path=$PWD
#remove_pck=./remove_pck.txt

echo "1.Start environment prepare"
dnf -y install tar
rm -rf $imagetool_path
mkdir $imagetool_path
mkdir $rootfs_path
echo "Environment prepared"
echo

echo "2.Create rootfs"
#for pkg in `cat $packages_path`
#do
#dnf --installroot=$rootfs_path -y --nogpgcheck install $pkg
dnf --installroot=$rootfs_path -y --nogpgcheck install gnupg2 network-scripts dnf
#done
dnf autoremove
dnf clean all
echo "Rootfs created successfully"
echo

#echo "3.Remove useless packages in rootfs"
#for pkg in $remove_pck
#do
#echo "Removing $pkg" 
#rpm --root $rootfs_path -e -nodeps $pkg
#done
#echo "Packages removed successfully"

echo "3.Remove useless director"
rm -rf $rootfs_path/boot
rm -rf $rootfs_path/var/cache/dnf
rm -rf $rootfs_path/var/log
echo "Director removed"

echo "4.Pack the tarball"
cd $rootfs_path
tar -cvf openeuler_base.tar *
mv openeuler_base.tar $present_path
cd $present_path
echo 

echo "5.Clean"
rm -rf $imagetool_path

echo "Finished"
